<template>
 <div id="detail">
   <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"/>
   <scroll class="content"
           ref="scroll"
           :probe-type="3"
           @scroll="contentScroll">
     <!-- :top-images 传入值: top-images-->
     <detail-swiper :top-images="topImages"/>
     <detail-base-info :goods="goods"/>
     <detail-shop-info :shop="shop"/>
     <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
     <detail-param-info ref="params" :param-info="paramInfo"/>
     <detail-comment-info ref="comment" :comment-info="commentInfo"/>
     <goods-list ref="recommend" :goods="recommends"/>

   </scroll>

   <detail-bottom-bar @addCart="addToCart"/>

   <back-top @click.native="backClick" v-show="isShowBackTop" />
<!--   <toast :message="message" :show="show"/>-->
 </div>
</template>

<script>
  import DetailNavBar from './childComps/DetailNavbar'
  import DetailSwiper from './childComps/DetailSwiper'
  import DetailBaseInfo from "@/views/detail/childComps/DetailBaseInfo";
  import DetailShopInfo from "@/views/detail/childComps/DetailShopInfo";
  import DetailGoodsInfo from "@/views/detail/childComps/DetailGoodsInfo";
  import DetailParamInfo from "@/views/detail/childComps/DetailParamInfo";
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailBottomBar from "@/views/detail/childComps/DetailBottomBar";

  import Scroll from "components/common/scroll/Scroll";
  import GoodsList from "@/components/content/goods/GoodsList";
  // import Toast from "@/components/common/toast/Toast";



  import {getDetail,Goods,Shop,GoodsParam,getRecommend} from "@/network/detail";
  import {itemListenerMixin,backTopMixin} from "common/mixin";
  import {debounce} from "@/common/utils";

  import { mapActions} from 'vuex'


  export default {
    name: "detail",
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,

      Scroll,
      GoodsList,
      // Toast
    },
    mixins:[itemListenerMixin, backTopMixin],
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommends: [],
        themeTopYs:[],
        getThemeTopY:null,
        currentIndex: 0,
        message: '',
        show: false
      }
      },
    created() {
      //1.获取id
      this.iid = this.$route.params.iid

      //2.根据id请求详情数据
      getDetail(this.iid).then(res => {
        // console.log(res)
        const data = res.result;

        //1.获取顶部的图片的轮播数据
        this.topImages = res.result.itemInfo.topImages

        //2.获取商品信息
        this.goods = new Goods(data.itemInfo,data.columns, data.shopInfo.services)

        //3.创建店铺信息的对象
        this.shop = new Shop(data.shopInfo)

        //4.保存商品的详情数据
        this.detailInfo =  data.detailInfo;

        //5.获取参数信息
        this.paramInfo = new  GoodsParam(data.itemParams.info, data.itemParams.rule)

        //6.取出评论的信息
        if (data.rate.cRate !== 0 ) {
          this.commentInfo = data.rate.list[0]
        }
        /*
        this.themeTopYs = []
        //1.第一次获取,值不对
        // 值不对的原因：this.$refs.params.$el 压根没有渲染

        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)

        console.log(this.themeTopYs)

        this.$nextTick(()  => {
          //2.值不对的原因:图片没有计算在内
          //根据最新的数据，对应的DOM是已经被渲染出来
          //但是图片依然是没有加载完成(目前获取到offsetTop不包含其中的图片)
          //offsetTop值不对的时候，都是因为图片有问题
          this.themeTopYs = []

          this.themeTopYs.push(0);
          this.themeTopYs.push(this.$refs.params.$el.offsetTop)
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
          this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)

          console.log(this.themeTopYs)

        })
        */
      })

      //3.请求推荐数据
      getRecommend().then(res => {
        this.recommends = res.data.list
      })
      //4.给getThemeTopy赋值(给this.themeTopYs赋值的操作进行防抖)
      this.getThemeTopY = debounce(() => {
        //console.log('-----')
        this.themeTopYs = []
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        this.themeTopYs.push(Number.MAX_VALUE)

        //console.log(this.themeTopYs)

      })
    },
    // methods: {
    //   imageLoad(){
    //     this.$refs.scroll.refresh()
    //
    //   }
    // }
    //4.给getThemeTopY赋值
    destroyed() {
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    },

    //解决详情页有时候不能滚动的bug
    methods: {
      ...mapActions(['addCart']),
      imageLoad(){
        this.refresh()
        this.getThemeTopY()

      },

      titleClick(index){
        //console.log(index)
        this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],200)

      },
      //实现滚动内容显示对应标题
      contentScroll(position){
        //1.获取y值
        const positionY = -position.y

        //2.positionY和主题中的值进行对比
        let length = this.themeTopYs.length
         for(let i = 0; i< length-1; i++) {
           // console.log(i+1);
           // if (positionY > this.themeTopYs[parseInt(i)] && positionY < this.themeTopYs[i+1]){
           //   console.log(i)

           if (this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
             this.currentIndex = i;
             this.$refs.nav.currentIndex = this.currentIndex
           }
           //   console.log(i)
           //   if (this.currentIndex !== i && (i <length -1 && positionY >= this.themeTopYs[(i)] && positionY <
           //     this.themeTopYs[i+1] )|| ( i ===length -1 && positionY > this.themeTopYs[(i)])) {
           //     this.currentIndex = i;
           //     //console.log(this.currentIndex)
           //     this.$refs.nav.currentIndex = this.currentIndex
           // }


           //3.点击按钮是否显示回到顶部
           this.isShowBackTop = (-position.y) > 1000

         }
      },
      addToCart() {
        //1.获取购物车需要展示的信息
        const product = { }
        product.image = this.topImages[0]
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.realPrice;
        product.iid = this.iid;

        //2.将商品添加到购物车里(1.Promise 2.mapActions)
        // this.$store.cartList.push(product)
        // this.$store.commit('addCart',product)
        this.addCart(product).then(res => {
          //   this.show = true;
          //   this.message = res;
          //
          //   setTimeout(() => {
          //     this.show = false;
          //     this.message = ''
          //   },1500)
          // })
          this.$toast.show()
          console.log(this.$toast)
        })

        // this.$store.dispatch('addCart', product).then(res => {
        //   console.log(res)
        // })


      }

    }
  }
</script>

<style scoped>
 #detail {
   position: relative;
   z-index: 9;
   background-color: #fff;
   height: 100vh;
 }
 .detail-nav{
   position: relative;
   z-index: 9;
   background-color: #fff;
 }


 .content {
   height: calc(100% - 44px - 49px);

 }
</style>
